package com.seselin.stepdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int stepIndex = 0;
    private int maxIndex = 3;

    private StepView[] step = new StepView[maxIndex + 1];
    private TextView[] tvStep = new TextView[maxIndex + 1];
    private StepProcess[] stepPro = new StepProcess[maxIndex];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.tvStep[3] = (TextView) findViewById(R.id.tv_step_4);
        this.tvStep[2] = (TextView) findViewById(R.id.tv_step_3);
        this.tvStep[1] = (TextView) findViewById(R.id.tv_step_2);
        this.tvStep[0] = (TextView) findViewById(R.id.tv_step_1);
        this.step[3] = (StepView) findViewById(R.id.step_4);
        this.step[2] = (StepView) findViewById(R.id.step_3);
        this.step[1] = (StepView) findViewById(R.id.step_2);
        this.step[0] = (StepView) findViewById(R.id.step_1);
        this.stepPro[2] = (StepProcess) findViewById(R.id.step_pro_3);
        this.stepPro[1] = (StepProcess) findViewById(R.id.step_pro_2);
        this.stepPro[0] = (StepProcess) findViewById(R.id.step_pro_1);

        for (int i = 0; i < maxIndex + 1; i++) {
            step[i].setStepIndex(i + 1);
            tvStep[i].setTextColor(Color.DISABLE_GRAY);
        }

        step[0].setState(StepView.STEP_DOING);
        tvStep[0].setTextColor(Color.GREEN);
    }


    public void btnNext(View view) {
        if (stepIndex < maxIndex)
            stepIndex++;

        refreshStep();
    }

    public void btnBack(View view) {
        if (stepIndex > 0)
            stepIndex--;

        refreshStep();
    }

    private void refreshStep() {
        for (int i = 0; i < maxIndex + 1; i++) {
            if (i < stepIndex) {
                step[i].setState(StepView.STEP_DONE);
                step[i].invalidate();
                tvStep[i].setTextColor(Color.GREEN);
            } else if (i == stepIndex) {
                step[i].setState(StepView.STEP_DOING);
                step[i].invalidate();
                tvStep[i].setTextColor(Color.GREEN);
            } else {
                step[i].setState(StepView.STEP_UNDO);
                step[i].invalidate();
                tvStep[i].setTextColor(Color.DISABLE_GRAY);
            }
        }

        for (int i = 0; i < maxIndex; i++) {
            if (i < stepIndex) {
                stepPro[i].setState(StepProcess.STEP_DONE);
                stepPro[i].invalidate();
            } else {
                stepPro[i].setState(StepProcess.STEP_UNDO);
                stepPro[i].invalidate();
            }
        }
    }
}
